const util = require('../../../utils/util.js');
const api = require('../../../config/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value1: 1,
    num: 1,
    address: {},
    postId: 0
  },
  onLoad: function(options) {
    if (options.postId) {
      this.setData({
        postId: options.postId
      });
    }
    this.getBuyNum();
    if (this.data.postId == 0) {
      this.getDefaultPost();
    } else {
      this.getPost();
    }
  },
  getBuyNum: function () {
    let that = this;
    util.request(api.GetBuyNumUrl).then(function (res) {
      if (res.success) {
        that.setData({
          num: res.data
        });
      }
    });
  },
  handleChange1({
    detail
  }) {
    this.setData({
      value1: detail.value
    })
  },
  selectAddress: function() {
    wx.navigateTo({
      url: '/pages/vip/address/address'
    })
  },
  getDefaultPost: function() {
    let that = this;
    util.request(api.DefaultPostUrl).then(function(res) {
      if (res.success) {
        that.setData({
          address: res.data
        });
      }
    });
  },
  getPost: function() {
    let that = this;
    util.request(api.GetPostUrl, {
      id: that.data.postId
    }).then(function(res) {
      if (res.success) {
        that.setData({
          address: res.data
        });
      }
    });
  },
  toCustom: function() {
    let that = this;
    util.requestPost(api.CustomCardUrl, {
      postId: that.data.address.id,
      num: that.data.value1
    }).then(function(res) {
      if (res.success) {
        wx.navigateTo({
          url: '/pages/card/cardInfo/cardInfo?id=' + res.data
       })
        wx.showToast({
          title: res.message
        })
      }
      wx.showToast({
        title: res.message
      })
    });
  }
})